﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
using Models;
using Newtonsoft.Json.Linq;

namespace GoShopping.tools
{
    /// <summary>
    /// Orderds 的摘要说明
    /// </summary>
    public class Orderds : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string t = context.Request["t"].ToString();
            //0:添加
            //1:查询，根据UserID
            //2:联表查询 Order_Goods表
            //3:删除
            //4:更改评论状态

            switch (t)
            {
                case "0":
                    addOrder(context);
                         break;
                case "1":
                    getAll(context);
                    break;
                case "2":
                    getOrder_Goods(context);
                    break;
                case "3":
                    delete(context);
                    break;
                case "4":
                    updateCon(context);
                    break;
            }
        }
        //更改评论状态
        private void updateCon(HttpContext context)
        {
            int OrderID = int.Parse(context.Request["OrderID"]);
            bool res = OrderBLL.updateConmmentBLL(OrderID);
            if (res)
            {
                context.Response.Write("{\"status\":\"修改状态成功！\"}");
            }
            else
            {
                context.Response.Write("{\"status\":\"修改状态失败！\"}");
            }
        }

        //删除
        private void delete(HttpContext context)
        {
            int OrderID = int.Parse(context.Request["OrderID"]);
            bool res=OrderBLL.deleteByIDBLL(OrderID);
            if (res)
            {
                context.Response.Write("{\"status\":\"删除成功！\"}");
            }
            else
            {
                context.Response.Write("{\"status\":\"删除失败！\"}");
            }
        }

        //联表查询 Order_Goods表
        private void getOrder_Goods(HttpContext context)
        {
            int UserID = int.Parse(context.Request["UserID"]);
            List<Order_Goods> list=OrderBLL.GetOrder_GoodsBLL(UserID); ;
            //把数据转换为JSON格式
            JObject obj = new JObject();
            JArray array = JArray.FromObject(list);
            obj.Add("res", array);
            if (list.Count > 0)
            {

                context.Response.Write(obj);
            }
            else
            {
                context.Response.Write("{\"status\":\"连接服务器失败！\"}");
            }
        }

        //查询，根据UserID
        private void getAll(HttpContext context)
        {
            int UserID = int.Parse(context.Request["UserID"]);
            List<Order> list=  OrderBLL.getAllByUserBLL(UserID);
            //把数据转换为JSON格式
            JObject obj = new JObject();
            JArray array = JArray.FromObject(list);
            obj.Add("res", array);
            if (list.Count > 0)
            {

                context.Response.Write(obj);
            }
            else
            {
                context.Response.Write("{\"status\":\"连接服务器失败！\"}");
            }
        }

        //添加订单
        private void addOrder(HttpContext context)
        {
            Order order = new Order();
            order.UserID = int.Parse(context.Request["UserID"]);
            order.GoodsID= int.Parse(context.Request["GoodsID"]);
            order.Num = int.Parse(context.Request["Num"]);
            order.Address = context.Request["Address"].ToString();
            order.OrderMoney = Convert.ToDecimal(context.Request["OrderMoney"]);
            order.AddressName = Convert.ToString(context.Request["AddressName"]);
            order.AddressPhone = context.Request["AddressPhone"].ToString();
            bool res=OrderBLL.addOrderBLL(order);
            if (res)
            {
                context.Response.Write("{\"status\":\"支付成功！\"}");
            }else
            {
                context.Response.Write("{\"status\":\"支付失败！\"}");
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}